package com.ninjas.module.error.controller;

import com.ninjas.module.common.page.PageResult;
import com.ninjas.module.error.domain.dto.ErrorStatisticsDTO;
import com.ninjas.module.error.domain.model.Error;
import com.ninjas.module.error.domain.model.UserOptLog;
import com.ninjas.module.error.domain.vo.IndexErrorStatisticsVO;
import com.ninjas.module.error.enums.OptType;
import com.ninjas.module.error.service.ErrorService;
import com.ninjas.module.error.service.StatisticsService;
import com.ninjas.module.error.service.UserOptLogService;
import com.ninjas.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RequestMapping("/index")
@RestController
public class IndexController {

    @Autowired
    private ErrorService errorService;
    @Autowired
    private StatisticsService statisticsService;
    @Autowired
    private UserOptLogService userOptLogService;

    @GetMapping("/error/statistics")
    public IndexErrorStatisticsVO errorStatistics() {
        LocalDateTime now = LocalDateTime.now();
        ErrorStatisticsDTO all = statisticsService.indexError(null, null);
        ErrorStatisticsDTO year = statisticsService.indexError(DateUtils.getYearStart(now), DateUtils.getYearEnd(now));
        ErrorStatisticsDTO month = statisticsService.indexError(DateUtils.getMonthStart(now), DateUtils.getMonthEnd(now));
        ErrorStatisticsDTO day = statisticsService.indexError(DateUtils.getDayStart(now), DateUtils.getDayEnd(now));
        return IndexConverter.toIndexErrorStatisticsVO(all, year, month, day);
    }

    @GetMapping("/error/list")
    public PageResult errorList() {
        List<Error> list = errorService.errorPageList();
        return IndexConverter.toIndexErrorListVO(list);
    }

    @GetMapping("/opt/list")
    public PageResult optList() {
        List<UserOptLog> list = userOptLogService.pageList(OptType.USER);
        return IndexConverter.toIndexUserOptLogListVO(list);
    }

    @GetMapping("/opt/model/list")
    public PageResult optModelList() {
        List<UserOptLog> list = userOptLogService.pageList(OptType.MODEL_UPDATE);
        return IndexConverter.toIndexUserOptLogListVO(list);
    }
}